package com.holdtime.jtt809dataservice.business.hd.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.holdtime.common.utils.general.ApiResponseModel;
import com.holdtime.jtt809dataservice.business.api.ClientLinkService;
import com.holdtime.jtt809dataservice.business.hd.BusinessHdCoast;
import com.holdtime.jtt809dataservice.business.hd.entity.DsParentPlatformDO;
import com.holdtime.jtt809dataservice.business.hd.entity.mapper.DsParentPlatformMapper;
import com.holdtime.jtt809dataservice.common.bean.*;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * author: sunwei
 * Date: 2025/3/19 22:17
 * Description:
 */
@Slf4j
@Service
public class ClientLinkServiceImpl implements ClientLinkService {
    @Resource
    private DsParentPlatformMapper dsParentPlatformMapper;

    @Override
    public ApiResponseModel<?> dealUpConnectRsp(ClientUpConnectRspDTO clientUpConnectRspDTO) {
        ApiResponseModel<?> model = new ApiResponseModel<>();
        String str = "";
        try {
            //TODO
            int result = clientUpConnectRspDTO.getResult();
            DsParentPlatformDO dsParentPlatformDO = dsParentPlatformMapper.selectOne(
                    Wrappers.<DsParentPlatformDO>lambdaQuery()
                            .eq(DsParentPlatformDO::getJrm, String.valueOf(clientUpConnectRspDTO.getJrm()))
            );
            if (dsParentPlatformDO != null) {
                dsParentPlatformDO.setConnectStatus(BusinessHdCoast.PARENT_PLATFORM_CONNECT_STATUS_LJ);
                dsParentPlatformDO.setParentPlatfromResponse(result);
                dsParentPlatformDO.setParentPlatformJym(clientUpConnectRspDTO.getVeryifyCode());
                switch (result) {
                    case 0:
                        dsParentPlatformDO.setParentMainLinkStatus(BusinessHdCoast.PARENT_PLATFORM_MAIN_LINK_STATUS_ZC);
                        break;
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                        dsParentPlatformDO.setParentMainLinkStatus(BusinessHdCoast.PARENT_PLATFORM_MAIN_LINK_STATUS_YC);
                        break;
                    case 5:
                    case 6:
                        dsParentPlatformDO.setParentMainLinkStatus(BusinessHdCoast.PARENT_PLATFORM_MAIN_LINK_STATUS_CS);
                        break;
                }
                dsParentPlatformMapper.updateById(dsParentPlatformDO);
            }
        } catch (Exception e) {
            // TODO: handle exception
            log.error(str, e);
        }
        if (StringUtils.hasText(str)) {
            model.error(str);
        } else {
            model.success();
        }
        return model;
    }

    @Override
    public ApiResponseModel<?> dealUpDisConnectInForm(ClientUpDisConnectInFormDTO clientUpDisConnectInFormDTO) {
        ApiResponseModel<?> model = new ApiResponseModel<>();
        String str = "";
        try {
            DsParentPlatformDO dsParentPlatformDO = dsParentPlatformMapper.selectOne(
                    Wrappers.<DsParentPlatformDO>lambdaQuery()
                            .eq(DsParentPlatformDO::getJrm, String.valueOf(clientUpDisConnectInFormDTO.getJrm()))
            );
            if (dsParentPlatformDO != null) {
                dsParentPlatformDO.setParentMainLinkStatus(BusinessHdCoast.PARENT_PLATFORM_MAIN_LINK_STATUS_DK);
                dsParentPlatformMapper.updateById(dsParentPlatformDO);
            }
        } catch (Exception e) {
            // TODO: handle exception
            log.error(str, e);
        }
        if (StringUtils.hasText(str)) {
            model.error(str);
        } else {
            model.success();
        }
        return model;
    }

    @Override
    public ApiResponseModel<Integer> dealDownConnectReq(ClientDownConnectReqDTO clientDownConnectReqDTO) {
        ApiResponseModel<Integer> model = new ApiResponseModel<>();
        String str = "";
        try {
            DsParentPlatformDO dsParentPlatformDO = dsParentPlatformMapper.selectOne(
                    Wrappers.<DsParentPlatformDO>lambdaQuery()
                            .eq(DsParentPlatformDO::getJrm, String.valueOf(clientDownConnectReqDTO.getJrm()))
            );
            if (dsParentPlatformDO != null) {
                if (clientDownConnectReqDTO.getJym() == dsParentPlatformDO.getParentPlatformJym().longValue()) {
                    model.setRecord(0);
                    dsParentPlatformDO.setParentSubLinkStatus(BusinessHdCoast.PARENT_PLATFORM_SUB_LINK_STATUS_YLC);
                    dsParentPlatformMapper.updateById(dsParentPlatformDO);
                } else {
                    model.setRecord(1);
                    dsParentPlatformDO.setParentSubLinkStatus(BusinessHdCoast.PARENT_PLATFORM_SUB_LINK_STATUS_YC);
                    dsParentPlatformMapper.updateById(dsParentPlatformDO);
                }
            }
        } catch (Exception e) {
            // TODO: handle exception
            log.error(str, e);
        }
        if (StringUtils.hasText(str)) {
            model.error(str);
        } else {
            model.success();
        }
        return model;
    }

    @Override
    public ApiResponseModel<?> dealDownDisConnectReq(ClientDownDisconnectReqDTO clientDownDisconnectReqDTO) {
        ApiResponseModel<?> model = new ApiResponseModel<>();
        String str = "";
        try {
            DsParentPlatformDO dsParentPlatformDO = dsParentPlatformMapper.selectOne(
                    Wrappers.<DsParentPlatformDO>lambdaQuery()
                            .eq(DsParentPlatformDO::getJrm, String.valueOf(clientDownDisconnectReqDTO.getJrm()))
            );
            if (dsParentPlatformDO != null) {
                if (clientDownDisconnectReqDTO.getJym() == dsParentPlatformDO.getParentPlatformJym().longValue()) {
                    dsParentPlatformDO.setParentSubLinkStatus(BusinessHdCoast.PARENT_PLATFORM_SUB_LINK_STATUS_WLJ);
                    dsParentPlatformMapper.updateById(dsParentPlatformDO);
                } else {
                    str = "校验码错误";
                }
            }
        } catch (Exception e) {
            // TODO: handle exception
            log.error(str, e);
        }
        if (StringUtils.hasText(str)) {
            model.error(str);
        } else {
            model.success();
        }
        return model;
    }

    @Override
    public ApiResponseModel<?> dealDownDisConnectInForm(ClientDownDisConnectInFormDTO clientDownDisConnectInFormDTO) {
        ApiResponseModel<?> model = new ApiResponseModel<>();
        String str = "";
        try {
            DsParentPlatformDO dsParentPlatformDO = dsParentPlatformMapper.selectOne(
                    Wrappers.<DsParentPlatformDO>lambdaQuery()
                            .eq(DsParentPlatformDO::getJrm, String.valueOf(clientDownDisConnectInFormDTO.getJym()))
            );
            if (dsParentPlatformDO != null) {
                dsParentPlatformDO.setParentSubLinkStatus(BusinessHdCoast.PARENT_PLATFORM_SUB_LINK_STATUS_WLJ);
                dsParentPlatformMapper.updateById(dsParentPlatformDO);
            }
        } catch (Exception e) {
            // TODO: handle exception
            log.error(str, e);
        }
        if (StringUtils.hasText(str)) {
            model.error(str);
        } else {
            model.success();
        }
        return model;
    }
}
